var app = getApp();

Page({
    data: {
        proId:"",  //产品id
        categoryId:"",
        imgUrl:app.globalData.imgUrl,
        systemInfo: {},
        orderInfo:"",
        padBtm:"25",
        shadowFlag: false,
    },
    onLoad: function (e) {
        var that=this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    systemInfo: res,
                })
                if(res.model == "iPhone X"){
                    that.setData({
                        padBtm:"50",
                    })
                }
            }
        });
        that.setData({
           navH: app.globalData.navHeight,
           proId:e.id,
        })
        that.getProDet();
        that.getUserInfo();
    },
    onShareAppMessage() {
        var that = this;
        return {
          title: '在海花岛这样订房也不错，快看快约！',
          //path: '/pages/activity/confirm/activity-confirm?id='+that.data.proId
          path: 'pages/index/index?url=/pages/activity/confirm/activity-confirm&id=' + that.data.proId
        }
    },
    getProDet:function(){
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/index/goodsDetails",
            data: {
                id: that.data.proId
            },
            header: {  
                'content-type': 'application/x-www-form-urlencoded' 
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var orderData = res.data.data;
                    console.log(orderData);
                    that.setData({
                        orderInfo: orderData,
                        categoryId:orderData.category_id
                    })
                }
            },
            
        });
    },
    getUserInfo: function () {
        var that = this;
        wx.request({
            url: app.globalData.apiHost + "/api/user/info",
            data: {
                token: wx.getStorageSync('token')
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': 'token=' + wx.getStorageSync('token')
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode === 200) {
                    var userData = res.data.data;
                    that.setData({ 
                        email: userData.email,
                        mobile: userData.mobile,
                    })
                }
            },
            
        });
    },
    navBack: function () {
        wx.navigateBack({
          delta: 1
        })
    },
    mobileInput:function(e){
        this.setData({
            mobile: e.detail.value
        })
    },
    emailInput:function(e){
        this.setData({
            email: e.detail.value
        })
    },
    orderSubmit: function (e) {
        let formId = e.detail.formId;
        var that=this;
        var telReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        var emailRge = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        if (e.detail.value.mobile.length == 0 || e.detail.value.email.length == 0) {
            wx.showToast({
                title: '手机号码和邮箱不得为空',
                icon: 'none',
                duration: 1500
            })
        }else if(e.detail.value.mobile.length!=11){
            wx.showToast({
                title: '手机号码长度有误',
                icon: 'none',
                duration: 1500
            })
        }else if(!telReg.test(e.detail.value.mobile)){
            wx.showToast({
                title: '手机号码格式不正确',
                icon: 'none',
                duration: 1500
            })
        }else if(!emailRge.test(e.detail.value.email)){
            wx.showToast({
                title: '邮箱格式不正确',
                icon: 'none',
                duration: 1500
            })
        }else{
          wx.request({
              url: app.globalData.apiHost + "/api/order/saveOrder",
              data: {
                  goods_id: that.data.proId,
                  order_type:that.data.categoryId,
                  mobile:e.detail.value.mobile,
                  email:e.detail.value.email,
                  form_id: formId,
                  number:1
              },
              header: {
                  'content-type': 'application/x-www-form-urlencoded',
                  'Cookie':'token='+wx.getStorageSync('token')
              },
              method: "POST",
              dataType: "json",
              success: function (res) {
                  if (res.statusCode === 200) {
                      that.setData({
                          shadowFlag: false,
                      })
                      wx.redirectTo({
                          url: '../../template/reserve/reserve?id=3&thumb='+that.data.orderInfo.thumb
                      })
                  }
              },
              fail: function (e) {
              },
              complete: function (obj) {
              }
          });
        }
    }
})
